package com.bjsxt;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**优化的点对点聊天应用*/

//发送消息线程
class SendMsg extends Thread{
    private Socket socket;
    Scanner scanner;
    public SendMsg(Socket socket,Scanner scanner){
        this.socket=socket;
        this.scanner=scanner;
    }

    @Override
    public void run() {
        this.send();
    }
    private void send(){

        PrintWriter pw = null;
        try{
            this.scanner =new Scanner(System.in);
            pw = new PrintWriter(this.socket.getOutputStream());
            while (true) {
                String keyInput = this.scanner.nextLine();
                pw.println(keyInput);
                pw.flush();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(this.scanner != null){
                this.scanner.close();
            }
            if(pw != null){
                pw.close();
            }
            if(this.socket != null){
                try {
                    this.socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
//接收消息线程
class ReceiveMsg extends Thread{
    private Socket socket;
    public ReceiveMsg(Socket socket){
        this.socket = socket;
    }
    @Override
    public void run() {
        this.receive();
    }
    private void receive(){
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(this.socket.getInputStream() ));
            while (true) {
                String str = br.readLine();
                System.out.println("他说：" + str);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(this.socket != null){
                try {
                    this.socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class GoodTcp {
    public static void main(String[] args) {
        Scanner scanner = null;//用户要输入是选择客服务端，还是服务端
        //Socket是在上面接收消息线程和发送消息关闭，主线程不可以关闭，否则断开连接
        Socket socket = null;
        ServerSocket serverSocket = null;//等下要关闭，Socket是在
        try{
            scanner = new Scanner(System.in);
            System.out.println("请输入：server,<port>或者：<ip>,<port>");
            String str = scanner.nextLine();
            //split对字符串以给定的字符进行分隔，得到字符串数组。split有两种定义方法。
            // 一种是split(String regex):String[]-string。regex是要分隔的字符。
            String[] arr = str.split(",");//请输入后面的内容是“,”隔开
            if("server".equals(arr[0])){
                //启动服务端
                System.out.println("Tcp Server Listen at "+arr[1]+".......");
                //输入的是String类型，现在转为整数类型
                serverSocket = new ServerSocket(Integer.parseInt(arr[1]));
                socket = serverSocket.accept();
                System.out.println("连接成功");

            }else {
                //启动客服端
                socket = new Socket(arr[0],Integer.parseInt(arr[1]));
                System.out.println("连接成功");
            }
            //启动发送消息线程
            new SendMsg(socket,scanner).start();
            //接收消息线程
            new ReceiveMsg(socket).start();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
